from django.http import JsonResponse
from django.shortcuts import render, redirect
from ks import models

def login(request):
	if request.method == 'GET':
		return render(request, 'templates/login.html')
	else:
		uname = request.POST.get('username')
		pwd = request.POST.get('password')
		res = models.UserInfo.objects.filter(username=uname, password=pwd)
		if res.exists():
			request.session['is_login'] = True
			request.session['username'] = uname
			part_name = res.first().part.all().values('name')
			request.session['part_name'] = list(part_name)
			return redirect('home')
		else:
			return redirect('login')

def home(request):
	name = request.session.get('username')
	user_obj = models.userimg.objects.filter(name=name).first()
	if request.method == 'GET':
		home_list = models.UserInfo.objects.all()
		return render(request, 'home.html',locals())
	elif request.method == 'POST':
		back_dic = {"code": 1000, 'msg': ''}
		print(name)
		file_obj = request.FILES.get('avatar')
		print(file_obj)
		user_obj.avatar = file_obj
		user_obj.save()
		back_dic["msg"] = "头像更新成功"
		return JsonResponse(back_dic)
def add_user(request):
	if request.method == 'GET':
		return render(request, 'add_user.html')
	else:
		sex = request.POST.get("sex")
		addr = request.POST.get("addr")
		user_d = models.UserDetail.objects.create(sex=sex, addr=addr)
		part_name = request.POST.get("part_name")
		res = models.Part.objects.create(name=part_name)
		username = request.POST.get("username")
		password = request.POST.get("password")
		phone_number = request.POST.get("phone_number")
		user = models.UserInfo.objects.create(
			username=username,
			password=password,
			phone_number=phone_number,
			ui=user_d
		)
		user.part.add(res)
		return redirect('/home/')

def add_part(request,user_id):
	if request.method == 'GET':
		return render(request, 'add_part.html')
	else:
		name = request.POST.get('name')
		if name!=None:
			res = models.Part.objects.create(name = name)
			user = models.UserInfo.objects.get(id=user_id)
			user.part.add(res)
		uname = request.POST.get('uname')
		if uname!=None:
			models.Part.objects.get(name=uname).delete()
		return redirect('/home/')

def edit_user(request,user_id):
	if request.method == 'POST':
		sex = request.POST.get("sex")
		addr = request.POST.get("addr")
		user_d = models.UserDetail.objects.filter(userinfo=user_id).update(sex=sex, addr=addr)
		username = request.POST.get("username")
		password = request.POST.get("password")
		phone_number = request.POST.get("phone_number")
		user = models.UserInfo.objects.filter(id=user_id).update(
			username=username,
			password=password,
			phone_number=phone_number,
		)
		return redirect('/home/')
	else:
		user = models.UserInfo.objects.get(id=user_id)
		user_d = models.UserDetail.objects.get(userinfo=user_id)
		part_n = models.Part.objects.filter(userinfo=user_id)
		return render(request, 'edit_user.html', locals())

def ajax_del_user(request, user_id):
	try:
		models.UserInfo.objects.get(id=user_id).delete()
		res_data = {'status': 1, 'msg': '删除成功!'}
	except:
		res_data = {'status': 0, 'msg': '删除失败!'}
	return JsonResponse(res_data)

def logout(request):
	request.session.flush()
	return redirect('login')